home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Includes / UPatch.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  4.4 KB  |  134 lines  |  [TEXT/MPS ]

  1. // UPatch.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __UPATCH__
  5. #define __UPATCH__
  6.  
  7. //----------------------------------------------------------------------------------------
  8. //  Theory of Operation
  9. //
  10. // This unit implements a general trap-patching mechanism. 
  11. //
  12. // The trap is patched such that it jumps to the routine of your choice, ignoring the
  13. // routine it previously jumped to. On a 128K ROM machine, the patch is made by setting
  14. // the trap address to your routine. On a 64K machine, a small block of code is
  15. // allocated in the system heap:The trap address is set to point to the block, and the
  16. // block contains code to jump to your routine.
  17. //
  18. // References to patches are kept in TrapPatch records. For each trap patched you must
  19. // define a variable of type TrapPatch. This unit links the TrapPatch records to form a
  20. // linked list to facilitate unpatching all patches without specifying each individual
  21. // patch.
  22. //----------------------------------------------------------------------------------------
  23.  
  24. // MacApp
  25.  
  26. #ifndef __MACAPPTYPES__
  27. #include "MacAppTypes.h"
  28. #endif
  29.  
  30.  
  31. //----------------------------------------------------------------------------------------
  32. // TrapPatch: Preferred. The pointer type _MUST_ be declared first since the record is
  33. // self referential
  34. //----------------------------------------------------------------------------------------
  35.  
  36. typedef struct TrapPatch *TrapPatchPtr;
  37.  
  38. struct TrapPatch
  39. {
  40.   protected:
  41.     short                trapNum;                // trap # being patched
  42.     UniversalProcPtr    oldTrapAddr;            // old trap address
  43.     UniversalProcPtr    patchRoutine;            // new trap routine descriptor (created by subclasses
  44.                                                 // of class TrapPatch, cleaned up by TrapPatch)
  45.     TrapPatchPtr        nextPatch;                // next link in linked list of patches
  46.     
  47.   public:
  48.     TrapPatch();
  49.     
  50.     short PatchTrap(short theTrapNum, void* theRoutine);
  51.         // PatchTrap patches the given trap to theRoutine. 
  52.     
  53.     void LookupOldTrapAddress(short theTrapNum);
  54.     
  55.     TrapPatchPtr GetPreviousPatchPtr();
  56.     TrapPatchPtr GetNewerPatchPtr();
  57.     void UnpatchTrap();
  58.         // UnpatchTrap unpatches the given trap.
  59.     
  60.     static void UnpatchAll();
  61.         // UnpatchAll unpatches all traps.
  62.     
  63.     UniversalProcPtr GetOldTrapAddr()    { return oldTrapAddr; }
  64.  
  65.   protected:
  66.     //----------------------------------------------------------------------------------------
  67.     // The following variable is really private but is in the interface just in case you're
  68.     // doing something really weird and you really need it
  69.     //----------------------------------------------------------------------------------------
  70.     
  71.     static TrapPatchPtr pPatchList;
  72. };
  73.  
  74.  #if !qPowerPC
  75. //----------------------------------------------------------------------------------------
  76. // CallBack
  77. //----------------------------------------------------------------------------------------
  78.  
  79. struct CallBack
  80. {
  81.     short saveRtnAdd;                            // Internal use
  82.     short moveRefCon;                            // Internal use
  83.     long refCon;                                // the refCon that will be passed as last
  84.                                                 // parm
  85.     short targOffset;                            // Internal use
  86.     short jmpInst;                                // Internal use
  87.     void* jmpTarg;                                // Internal use
  88. };
  89.  
  90. typedef CallBack* CallBackPtr;
  91.  
  92. #endif
  93.  
  94. #if !qPowerPC
  95. //----------------------------------------------------------------------------------------
  96. // JmpInstructionTemplate: template for creating/patching JMP instructions.
  97. //----------------------------------------------------------------------------------------
  98.  
  99. struct JmpInstructionTemplate {
  100.     short            Jmp;            // jmp instruction
  101.     void*            Routine;        // address to jump to
  102. };
  103.  
  104. #endif
  105.  
  106. //----------------------------------------------------------------------------------------
  107. // External function declarations
  108. //----------------------------------------------------------------------------------------
  109.  
  110. void FlushCache();
  111.     // Flush the data and instruction cache.
  112.     
  113. #if !qPowerPC
  114. void PatchJmpInstruction(void* patchAddress, void* jumpAddress);
  115.     // Place a 'jmp jumpAddress' instruction at patchAddress.
  116. #endif
  117.  
  118. #if !qPowerPC
  119. void SetCallBack(void* targProc, long itsRefCon, CallBackPtr theCallBackPtr);
  120.     // Prepares a call back record for use. Stuffs in the target of the call back and the
  121.     // refcon to pass as an _additional_ and _last_ parameter to it. Useful for getting
  122.     // context when being called back from the toolBox. Also useful for avoiding use of
  123.     // globals when getting context.
  124. #endif
  125.  
  126. inline void UnpatchAll()
  127. {
  128.     TrapPatch::UnpatchAll();
  129. }
  130.     // UnpatchAll unpatches all traps.
  131.  
  132.  
  133. #endif
  134.